package com.bird.thread.base.sixstaus;

/**
 * @author: bird
 * @Data: 2021/6/29 17:34
 * @description: 展示： blocked waiting timeWaiting的状态。
 */
public class BlockedWaitingTimedWaiting implements Runnable {


    public static void main(String[] args) {

        BlockedWaitingTimedWaiting runnable = new BlockedWaitingTimedWaiting();
        Thread thread1 = new Thread(runnable);
        thread1.start();
        Thread thread2 = new Thread(runnable);
        thread2.start();

        try {
            Thread.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        // 打印Time_waiting状态，因为正在执行Thread.sleep(1000).
        System.out.println(thread1.getState());

        // 打印出BLOCKED状态 因为 thread2想拿到sync()锁拿不到
        System.out.println(thread2.getState());


        try {
            Thread.sleep(5300);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //打印waiting状态 因为执行了wait()
        System.out.println(thread1.getState());
    }


    @Override
    public void run() {
        sync();
    }

    private synchronized void sync() {
        try {
            Thread.sleep(1000);
            wait();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
